from glob import glob
from binascii import b2a_hex
import re

filenames = glob('*.dat')

if False: # 发送数据与原始接收数据比对
    for filename in filenames:
        fileA = open(filename, 'rb')
        fileB = open(filename.split('.')[0]+'.txt', 'w')

        s = b2a_hex(fileA.read()).decode()

        s2 = []
        for line in re.split('1acffc1d7e01', s):
            if line == '':
                continue
            a = (int(line[:4], base=16) + 2) * 2
            s2.append(line[4:a])
        s2 = ''.join(s2)
        for line in re.split('fc10c53d1c96ecd400000001', s2):
            if line == '':
                continue

            fileB.write(str(len(line)) + ':' + line + '\n')

        fileA.close()
        fileB.close()


if False: # 发送的原始数据与送入解码的数据比对
    for filename in filenames:
        fileA = open(filename, 'rb')
        fileB = open(filename.split('.')[0]+'.txt', 'w')

        s = b2a_hex(fileA.read()).decode()
        if 'Recv' in filename:
            for line in re.split('fc10c53d1c96ecd400000001', s):
                if line == '':
                    continue
                fileB.write(line + '\n')
                # fileB.write(str(len(line)) + '\n')

        if 'Send' in filename:
            s2 = []
            for line in re.split('1acffc1d7e01', s):
                if line == '':
                    continue
                a = (int(line[:4], base=16) + 2) * 2
                s2.append(line[4:a])
            s2 = ''.join(s2)
            for line in re.split('fc10c53d1c96ecd400000001', s2):
                if line == '':
                    continue
                fileB.write(line + '\n')
                # fileB.write(str(len(line)) + '\n')

        fileA.close()
        fileB.close()

if False: # 连续数
    for filename in filenames:
        fileA = open(filename, 'rb')
        fileB = open(filename.split('.')[0]+'.txt', 'w')
        
        s = b2a_hex(fileA.read()).decode()

        s2 = []
        for line in re.split('1acffc1d7e01', s):
            if line == '':
                continue
            a = (int(line[:4], base=16) + 2) * 2
            s2.append(line[4:a])
        s = ''.join(s2)

        i = 0
        for line in re.split('(\w{128})', s):
            if line == '':
                continue
            fileB.write(line + '\n')
            # i += 1
            # if i % 16 == 0:
            #     fileB.write('\n')
            #     i = 0

        fileA.close()
        fileB.close()

if False: # 编解码日志提取
    filenames = glob('_log*.txt')
    file = open(filenames[0], 'r')
    fileA = open('encode_log.txt', 'w')
    fileB = open('decode_log.txt', 'w')
    for line in re.split('\n', file.read()):
        if line == '':
            continue
        f = None
        if 'Encode' in line[:8]:
            f = fileA
        elif 'Decode' in line[:8]:
            f = fileB
        else:
            continue
        line = re.split('(\[.*\]).*( Frame length: .*), .*', line)
        f.write(line[2] + '\n')
    fileA.close()
    fileB.close()